package com.jhws.business.owner.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.jhws.business.owner.bo.OwnerConstant;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.util.DateUtil;
import com.jhws.util.GetId;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jhws.business.comm.bo.TblCommDimBo;

import com.jhws.sys.appUser.bo.TblSipAcountBindings;
import com.jhws.sys.appUser.bo.TblSipAcountDimBo;

import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.sys.menu.service.ITblMenuDimService;
import com.jhws.sys.appUser.service.ITblSipAcountDimService;
import com.jhws.sys.user.service.ITblUserDimService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 物业管理员相关的控制器
 */
@Controller
@RequestMapping("/man")
public class ManagerCtrl {

	@Resource
	protected ITblSipAcountDimService ISipAccountService;
	@Resource
	protected ITblCommDimService ICommService;
	@Resource
	protected ITblMenuDimService IMenuService;
	@Resource
	protected ITblUserDimService IUserService;
	@Resource
    protected ITblOwnerDimService IOwnerService;
    @Resource
    protected ITblSipAcountDimService ISipAccount;
	@RequestMapping("forSave.x")
	private String forSave() throws Exception {
		return "business/manager/forSave";
	}

	@RequestMapping("save.x")
    @ResponseBody
	private JsonRespData save(String ownerCode,String commCode, String commName,String Name, String Phone,
                              String password,String BindingDev) throws Exception {
	    IUserService.checkCommCodePermission(commCode);
        List<TblSipAcountDimBo> sList = ISipAccount.findTblSipAcountDimByWhere("where username = '"+Phone+"'");
        if(sList != null && sList.size()!=0 && !sList.get(0).getPassword().equals(ISipAccount.getPasswordMD5(Phone,password)))
            return new JsonRespData("",-1,"密码与已存在的账号匹配不正确!");
        TblOwnerDimBo owner = new TblOwnerDimBo();
        if(StringUtils.isEmpty(ownerCode)) {//新增管理员
            String where = "where comm_code = '"+commCode+"' and owner_mob = '"+Phone+"' and apply_type = '"+OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER+"'";
            List<TblOwnerDimBo> list = IOwnerService.findTblOwnerDimByWhere(where);
            if(list != null && list.size()!= 0)
                return new JsonRespData("",-1,"该手机号码的管理员已存在!");
            owner.setOwnerCode(GetId.GetId2());
        }else{ //修改管理员
            owner = IOwnerService.findTblOwnerDimById(ownerCode);
            ISipAccount.removeSipBinding(owner.getOwnerMob(),owner.getRoomInfoNum());
        }

		if(StringUtils.isNotEmpty(BindingDev))
			ISipAccount.addSipBinding(Phone,commCode+"0000000"+BindingDev,Global.BINDING_TYPE_MANAGER,0);

        owner.setCommCode(commCode);
        owner.setCommName(commName);
        owner.setOwnerName(Name);
        owner.setOwnerMob(Phone);
        owner.setApplyType(OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER);
        owner.setLastModify(DateUtil.getTimeNowFmtSec());
		if(StringUtils.isEmpty(BindingDev))
			BindingDev = "0";
		owner.setRoomInfoNum(commCode+"0000000"+BindingDev);
        IOwnerService.save(owner);
        return new JsonRespData("",1,"");
	}

	@RequestMapping("query.x")
	@ResponseBody
	public JsonRespData query(@RequestParam Map<String,Object> map) throws java.lang.Exception {
        String commCode = MapUtils.getString(map, "commCode", "");
        commCode = IUserService.checkCommCodePermission(commCode);
        map.put("commCode",commCode);
        return new JsonRespData("",1,IOwnerService.findManagerList(map));
	}

	@RequestMapping("remove.x")
    @ResponseBody
	private JsonRespData remove(String ownerCode,HttpServletRequest request) throws Exception {

	    TblOwnerDimBo owner = IOwnerService.findTblOwnerDimById(ownerCode);
		ISipAccount.removeSipBinding(owner.getOwnerMob(),owner.getRoomInfoNum());
        IOwnerService.removeDim(ownerCode);
        return new JsonRespData("",1,"");
	}


	/**
	 * 获取某小区的管理机数目
	 * @param commCode 小区编号
	 * @return
	 */
	@RequestMapping("getManagerDevCount.x")
	@ResponseBody
	public JsonRespData getManagerDevCount(String commCode) {
		TblCommDimBo comm = ICommService.findTblCommDimById(commCode);
		return new JsonRespData("",1,comm.getManageDevNum());
	}

    /**
     * 临时函数,由于新的管理员角色写到了业主表，因此要将以前老的管理员绑定自动生成业主表信息
     * 下个版本移除该接口
     * @return
     */
    @RequestMapping("debugGenManagerForOldBindings.x")
    @ResponseBody
    public JsonRespData debugGenManagerForOldBindings() throws Exception{
        int count = 0;
	    List<TblSipAcountBindings> list = ISipAccount.findTblSipAcountBindingsByWhere("where binding_type = '"+Global.BINDING_TYPE_MANAGER+"'");
	    for(TblSipAcountBindings binding : list){
	        String commCode = binding.getBinding().substring(0,4);
	        TblCommDimBo comm = ICommService.findTblCommDimById(commCode);
	        List<TblOwnerDimBo> ownerList = IOwnerService.findTblOwnerDimByWhere("where owner_mob = '"+binding.getUsername()+"' and comm_code = '"+commCode+"' and apply_type = '"+OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER+"'");
	        if(ownerList == null || ownerList.size() == 0){
	            TblOwnerDimBo owner = new TblOwnerDimBo();
	            owner.setOwnerCode(GetId.GetId2());
	            owner.setApplyType(OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER);
	            owner.setCommName(comm.getCommName());
	            owner.setRoomInfoNum(binding.getBinding());
	            owner.setOwnerMob(binding.getUsername());
	            owner.setLastModify(DateUtil.getTimeNowFmtSec());
	            owner.setCommCode(commCode);
	            IOwnerService.save(owner);
                count++;
            }
        }
        return new JsonRespData("",1,"生成"+count+"条数据!");
    }
}
